<template>
  <div>
    <FormPanel ref="formpanel" :list="formList" :cols="1" :isSubmitBtn="false" />
    <div style="text-align: center;">
      <el-button type="primary" @click="saveBtn">保存</el-button>
      <el-button @click="cancelBtn">取消</el-button>
    </div>
  </div>
</template>

<script>
import { dictionary } from '@/utils/dictMixin';
import { Storage, queryUsablePartInfos, checkStorageMove } from '@/api/part/partOutbound/stockremoval/index';
import { jsonToHump } from '@/utils';
export default {
  name: 'BatchMove',
  mixins: [dictionary],
  props: {
    storageItemsList: {
      type: Array
    }
  },
  data() {
    this.queryStorage();
    return {
      formList: this.createList(),
      queryData: {},
      storageList: []
    };
  },
  mounted() {
    this.getstorageList();
  },
  methods: {
    // 翻译storage
    getstorageList() {
      for (const key in this.storageItemsList) {
        this.storageList[this.storageItemsList[key].value] = this.storageItemsList[key].text;
      }
      // console.log(this.storageList, 23213);
    },
    createList() {
      return [
        {
          type: 'SELECT',
          label: '原仓库',
          fieldName: 'OLD_STORAGE_CODE',
          rules: { required: true, message: '请选择原仓库', trigger: 'change' },
          itemList: this.storageItemsList
        },
        {
          type: 'SELECT',
          label: '新仓库',
          fieldName: 'NEW_STORAGE_CODE',
          rules: { required: true, message: '请选择新仓库', trigger: 'change' },
          itemList: this.storageItemsList
        }
      ];
    },
    async queryStorage() {
      // const res = await Storage();
      // // console.log(11111, res);
      // this.formList[0].itemList = res.data.map(x => ({ text: x.STORAGE_NAME, value: x.STORAGE_CODE }));
      // this.formList[1].itemList = res.data.map(x => ({ text: x.STORAGE_NAME, value: x.STORAGE_CODE }));
    },
    async saveBtn() {
      try {
        let value = this.$refs.formpanel.SUBMIT_FORM();
        jsonToHump(value);
        // console.log(value);

        if (value.oldStorageCode == value.newStorageCode) {
          this.$notify.error({ title: '提示信息', message: '新旧仓库不能一样' });
        }
        let oldOemTag;
        let newOemTag;
        this.storageItemsList.forEach(element => {
          if (element.value == value.oldStorageCode) {
            oldOemTag = element.tag;
          }
          if (element.value == value.newStorageCode) {
            newOemTag = element.tag;
          }
        });
        if (oldOemTag != newOemTag) {
          return this.$notify.warning({ title: '警告', message: 'OEM库与非OEM库之间不能移库！' });
        }
        // let result = await checkStorageMove(value);
        // if (result.data.isCan != '10041001') {
        // 可以出库
        let res = await queryUsablePartInfos(value);
        let saveData = [];
        // console.log(this.storageList, 123213);

        if (res.data.length) {
          res.data.forEach(element => {
            element.NEW_STORAGE_NAME = this.storageList[element.NEW_STORAGE_CODE];
            if (element.TRANSFER_QUANTITY) {
              saveData.push(element);
            }
          });
        }
        // console.log(res.data.length, saveData.length);
        // console.log(saveData);

        this.$emit('closeThree', res.data);
        // } else {
        //   return this.$notify.error({ title: '提示信息', message: '不可以出库' });
        // }
      } catch (error) {}
    },
    cancelBtn() {
      this.$emit('closeThree');
    }
  }
};
</script>

<style></style>
